# Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: https://docs.scrapy.org/en/latest/topics/item-pipeline.html
import hashlib
import json
import os
from pathlib import Path

import requests
# useful for handling different item types with a single interface
from itemadapter import ItemAdapter
from scrapy.exceptions import DropItem
from scrapy.http.request import NO_CALLBACK, Request
from scrapy.pipelines.images import ImagesPipeline
from scrapy.utils.python import to_bytes

from douban.items import ImageItem, MovieItem
from douban.spiders.meitu import MeituSpider
from douban.spiders.movie import MovieSpider
from douban.spiders.rosimm import RosimmSpider


class DoubanPipeline(ImagesPipeline):

    def get_media_requests(self, item, info):
        return Request(item['img_src'], headers={'referer':item['referer']})

    def file_path(self, request, response=None, info=None, *, item=None):
        image_guid = hashlib.sha1(to_bytes(request.url)).hexdigest()
        return f"{item['title']}/{image_guid}.jpg"

    def process_item(self, item, spider):
        if isinstance(item, ImageItem):
            return super().process_item(item, spider)
        return item

class DoubanMoviePipeline:
    def open_spider(self, spider):
        if isinstance(spider, MovieSpider):
            self.file = open("items.jsonl", "w")

    def close_spider(self, spider):
        if isinstance(spider, MovieSpider):
            self.file.close()

    def process_item(self, item, spider):
        if isinstance(spider, MovieSpider):
            line = json.dumps(ItemAdapter(item).asdict()) + "\n"
            self.file.write(line)
        return item
